<template>
	<drawer :show="show" :showTimeSelect="selectTimePopup" @closeSelect="closeSelectTimePopup" ref="drawer" @cancel="cancel"
		@confirm="confirm" @close="show = false" :cancelEvent="false" :title="'筛选'" cancelText="重置">
		<view class="filter-content">
			<!-- 预警标签 -->
			<filter-item v-model="selectedWarningTags" :show="type == 'confirmed' || type == 'all'" label="预警标签"
				:options="warningTagOptions" :multiple="true" @change="onWarningTagsChange" />

			<!-- 调度状态 -->
			<filter-item v-model="selectedDispatchStatus" :show="type == 'all'" label="调度状态"
				:options="dispatchStatusOptions" :multiple="true" @change="onDispatchStatusChange" />

			<!-- 工单状态 -->
			<filter-item v-model="selectedOrderStatus" :show="type == 'confirmed' || type == 'all'" label="工单状态"
				:options="orderStatusOptions" :multiple="true" @change="onOrderStatusChange" />

			<!-- 报警时间 -->
			<filter-item v-model="selectedAlarmTime" :show="true" label="报警时间" :options="alarmTimeOptions"
				:multiple="false" :customOption="{ label: '自定义' }" :customSelected="selectedAlarmTime === 'other'"
				@change="onAlarmTimeChange" @custom-select="openTimeSelect" />

			<!-- 报警来源 -->
			<filter-item v-model="selectedAlarmSource" :show="true" label="报警来源" :options="alarmSourceOptions"
				:multiple="true" @change="onAlarmSourceChange" />

			<!-- 误报确认 -->
			<filter-item v-model="selectedFalseAlarm" :show="type == 'confirmed' || type == 'all'" label="误报确认"
				:options="falseAlarmOptions" :multiple="true" @change="onFalseAlarmChange" />

			<!-- 调度是否超时 -->
			<filter-item v-model="selectedDispatchTimeout" :show="type == 'pending'" label="调度是否超时"
				:options="dispatchTimeoutOptions" :multiple="false" @change="onDispatchTimeoutChange" />
		</view>
	</drawer>
</template>
<script>
import drawer from '@/components/filter-drawer/drawer.vue';
import FilterItem from '@/components/filter-drawer/filter-item.vue';
import dayjs from 'dayjs';
import { dictionaryTree } from '@/apis/elevator.js';

export default {
	components: {
		drawer,
		FilterItem
	},
	props: {
		type: {
			type: String,
			default: '请选择'
		},
		title: {
			type: String,
			default: '请选择'
		},
	},
	data() {
		return {
			selectTimePopup: false,
			// 预警标签
			warningTagOptions: [
				{ label: '未及时调度', value: '4' },
				{ label: '未及时出发', value: '5' },
				{ label: '即将超时到达', value: '1' },
				{ label: '超时未达', value: '2' },
				{ label: '超时到达', value: '3' },
			],
			selectedWarningTags: [],
			// 调度状态
			dispatchStatusOptions: [
				{ label: '待调度', value: '0' },
				{ label: '已调度', value: '1' },
			],
			selectedDispatchStatus: [],
			// 工单状态
			orderStatusOptions: [
				// { label: '确认中', value: '确认中' },
				{ label: '已出发', value: '3' },
				{ label: '已到达', value: '4' },
				{ label: '已救援', value: '5' },
			],
			selectedOrderStatus: [],
			// 报警时间
			alarmTimeOptions: [
				{ label: '今天', value: 'day' },
				{ label: '本周', value: 'week' },
				{ label: '本月', value: 'month' },
			],
			timeVals: [],
			selectedAlarmTime: '',
			// 报警来源
			alarmSourceOptions: [

			],
			selectedAlarmSource: [],
			// 误报确认
			falseAlarmOptions: [
				{ label: '准确无误', value: '准确无误' },
				{ label: '误报', value: '误报' },
				{ label: '无法确认', value: '无法确认' },
			],
			selectedFalseAlarm: [],
			// 调度是否超时
			dispatchTimeoutOptions: [
				{ label: '是', value: '4' },
				{ label: '否', value: '0,1,2,3,5' },
			],
			selectedDispatchTimeout: '',
			show:false,
		};
	},
	created() {
		this.getTrapFroms();
	},
	methods: {
		// 预警标签变化处理
		onWarningTagsChange(value) {
			this.selectedWarningTags = value;
		},

		// 调度状态变化处理
		onDispatchStatusChange(value) {
			this.selectedDispatchStatus = value;
		},

		// 工单状态变化处理
		onOrderStatusChange(value) {
			this.selectedOrderStatus = value;
		},

		// 报警时间变化处理
		onAlarmTimeChange(value) {
			this.selectedAlarmTime = value;
			// 处理时间值
			if (value === 'day') {
				const today = dayjs().format('YYYY-MM-DD');
				this.timeVals = [
					today + ' 00:00:00',
					today + ' 23:59:59'
				];
			} else if (value === 'week') {
				const startOfWeek = dayjs().startOf('week').format('YYYY-MM-DD');
				const endOfWeek = dayjs().endOf('week').format('YYYY-MM-DD');
				this.timeVals = [
					startOfWeek + ' 00:00:00',
					endOfWeek + ' 23:59:59'
				];
			} else if (value === 'month') {
				const startOfMonth = dayjs().startOf('month').format('YYYY-MM-DD');
				const endOfMonth = dayjs().endOf('month').format('YYYY-MM-DD');
				this.timeVals = [
					startOfMonth + ' 00:00:00',
					endOfMonth + ' 23:59:59'
				];
			} else {
				this.timeVals = [];
			}
		},

		// 报警来源变化处理
		onAlarmSourceChange(value) {
			this.selectedAlarmSource = value;
		},

		// 误报确认变化处理
		onFalseAlarmChange(value) {
			this.selectedFalseAlarm = value;
		},

		// 调度超时变化处理
		onDispatchTimeoutChange(value) {
			this.selectedDispatchTimeout = value;
		},

		closeSelectTimePopup(res) {
			console.log(res);

			if (res) {
				// res: 2025.07.17-2025.07.18
				let start = '';
				let end = '';
				if (res.indexOf('-') !== -1) {
					const timeArr = res.split('-');
					start = timeArr[0].replace(/\./g, '-') + ' 00:00:00';
					end = timeArr[1].replace(/\./g, '-') + ' 23:59:59';
				} else {
					const time = res.replace(/\./g, '-');
					start = time + ' 00:00:00';
					end = time + ' 23:59:59';
				}
				const dateArr = [start, end];
				this.selectedAlarmTime = 'other';
				this.timeVals = dateArr;
				console.log('日期数组:', dateArr);
				// 你可以在这里使用 dateArr
			}
			this.selectTimePopup = false;
		},

		openTimeSelect() {
			this.selectTimePopup = true;
		},
		getTrapFroms() {
			dictionaryTree({
				code: 'rescue'
			}).then(res => {
				let data = res.data || [];
				for (let i = 0; i < data.length; i++) {
					if (data[i].dictKey == 1) {
						this.alarmSourceOptions = data[i].children.map(item => {
							return { label: item.dictValue, value: item.dictKey };
						});
						break;
					}
				}
			});
		},
		open(filterData) {
			// 回显筛选项
			if (filterData) {
				if (Array.isArray(filterData.warningTags)) this.selectedWarningTags = [...filterData.warningTags];
				if (Array.isArray(filterData.dispatchStatus)) this.selectedDispatchStatus = [...filterData.dispatchStatus];
				if (Array.isArray(filterData.orderStatus)) this.selectedOrderStatus = [...filterData.orderStatus];
				if (typeof filterData.alarmTime === 'string') this.selectedAlarmTime = filterData.alarmTime;
				if (Array.isArray(filterData.alarmSource)) this.selectedAlarmSource = [...filterData.alarmSource];
				if (Array.isArray(filterData.falseAlarm)) this.selectedFalseAlarm = [...filterData.falseAlarm];
				if (typeof filterData.dispatchTimeout === 'string') this.selectedDispatchTimeout = filterData.dispatchTimeout;
			}
			this.show = true;
		},
		confirm() {
			// 收集所有筛选项的值，包括自定义日期
			const filterData = {
				warningTags: this.selectedWarningTags,
				dispatchStatus: this.selectedDispatchStatus,
				orderStatus: this.selectedOrderStatus,
				alarmTime: this.selectedAlarmTime,
				alarmSource: this.selectedAlarmSource,
				falseAlarm: this.selectedFalseAlarm,
				dispatchTimeout: this.selectedDispatchTimeout,
				// 新增自定义日期区间
				timeVals: this.timeVals
			};

			this.$emit('confirm', filterData);
			this.show = false;
		},

		cancel() {
			// 重置所有筛选项为"全部"状态
			this.selectedWarningTags = [];
			this.selectedDispatchStatus = [];
			this.selectedOrderStatus = [];
			this.selectedAlarmTime = '';
			this.timeVals = [];
			this.selectedAlarmSource = [];
			this.selectedFalseAlarm = [];
			this.selectedDispatchTimeout = '';
		}
	}
};
</script>
<style lang="scss" scoped>
.filter-content {
	padding: 0 30rpx;
}
</style>